<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		ScriptPlanet.eu															/
/	Site:			http://www.scriptplanet.eu												/
/	Email:			support@scriptplanet.eu													/
/	Support:		http://www.scriptplanet.eu/support										/
/	Copyright:		2011 (c) ScriptPlanet.eu												/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0001																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla security
defined('_JEXEC') or die;

## Import Joomla Data
JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
?>

<script>
	// Joomla Task Check
	Joomla.submitbutton = function(task) {
		if (task == 'event.cancel' || document.formvalidator.isValid(document.id('event-form'))) {
			Joomla.submitform(task, document.getElementById('event-form')); }
		else {alert("<?php echo JText::_('COM_JOOSAM_JSCRIPT_NOFIL'); ?>");}
		return false; }
</script>

<!-- Start Form -->
<form action="<?php echo JRoute::_('index.php?option=com_joosam&layout=edit&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="event-form" class="form-validate">
	<fieldset class="panelform jsamLegend" style="width:600px; float:left;">
		
		<legend><?php echo JText::_('COM_JOOSAM_VIEW_EVENT_SETINGS'); ?></legend>
		<ul class="adminformlist">
			<?php foreach($this->form->getFieldset('myevents') as $field): ?>
				<li style="margin:0;padding:0;min-height:30px; line-height:15px;"><?php echo $field->label;echo $field->input;?><span id="sp_<?php echo $field->id; ?>" ></span></li>
			<?php endforeach; ?>
		</ul>
	</fieldset>
	<div>
		<input type="hidden" name="task" value="" />
		<?php echo JHtml::_('form.token'); ?>
	</div>
</form>
<fieldset class="samEvents jsamLegend" style="width:260px;">
	
	<legend><?php echo JText::_('COM_JOOSAM_CAT_SELECTOR'); ?></legend> <?php 
	$samerr = $this->samcat;
	if($samerr[0] == 'error' or $samerr[0] == 'warning'){
		echo '<div style="color:red; font-size:14px;">'.$samerr[1].'</div>';
	}
	else { echo '
		<ul class="samEventsList" style="float:left;">';
		foreach($this->samcat as $c => $sam){
			$catname = utf8_encode($sam['name']);
			echo '
			<li style="margin:0;padding:0;min-height:26px;">
				<input class="samcats jse'.$c.'" type="checkbox" id="sct_'.$sam['ID'].'" onclick="addme();" value="\''.$sam['ID'].'\'," style="margin:0;"/>
				<span style="padding-left:3px;padding-right:20px;">'.$catname.'</span>
			</li>';
		} echo '
		</ul>';
				## COM_JOOSAM_ALLOWALL
				## COM_JOOSAM_BLOCKALL
		echo '
		<ul class="adminformlist" style="float:right;">
			<li style="margin:0;padding:0;min-height:26px;">
				<input type="checkbox" id="allrequest" value="all" onclick="allowall();" style="margin:0;"/>
				<span style="padding-left:3px;padding-right:20px;">'.JText::_("COM_JOOSAM_ALLOWALL").'</span>
			</li>
			<li style="margin:0;padding:0;min-height:26px;">
				<input type="checkbox" id="blockrequest" value="none" onclick="blockall();" style="margin:0;"/>
				<span style="padding-left:3px;padding-right:20px;">'.JText::_("COM_JOOSAM_BLOCKALL").'</span>
			</li>
		</ul>';
	}
?>
</fieldset>
<div class="clr"></div>
<!-- If you have respect for scriptplanet.eu you don't delete the code below -->
<div class="spCopyFooter" style="padding:0;margin:0 auto;font-size:9px;width:220px;text-align:center;"><span>JooSam created by</span><a href="http://www.scriptplanet.eu">ScriptPlanet.eu</a><a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/3.0/"><img alt="Creative Commons Licentie" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-nd/3.0/80x15.png" /></a></div>

<script>/*
	##############################################################
	##	Extention:		JooSam									##
	##  Created by:		ScriptPlanet.eu							##
	##  Site:			http://www.scriptplanet.eu				##
	##  Email:			support@scriptplanet.eu					##
	##  Support:		http://www.scriptplanet.eu/support		##
	##  Copyright:		ScriptPlanet.eu 2011					##
	##	license:		CC BY-NC-ND 3.0                         ##
	##    (http://creativecommons.org/licenses/by-nc-nd/3.0/)   ##
	##  Joomla version:	1.7+									##
	##  JooSam version:	1.0										##
	##  Creation Date:	09/2011									##
	############################################################*/
	
	// Check Default Event
	function checkdefalt(){
		var src = document.getElementById('jform_isdef');
		if (src.value == 'true'){
			document.getElementById('jform_spends_img').setAttribute('style','display:none;');
			document.getElementById('jform_spstart_img').setAttribute('style','display:none;');
			document.getElementById('jform_spstart').setAttribute('readonly','true');
			document.getElementById('jform_spends').setAttribute('readonly','true');
			document.getElementById('jform_sprepeat0').setAttribute('checked','checked');
			document.getElementById('jform_sprepeat1').setAttribute('onclick','this.checked = false; alert("Not Allowed on default event"); jform_sprepeat0.checked = true;');
			document.getElementById('jform_sprepeat2').setAttribute('onclick','this.checked = false; alert("Not Allowed on default event"); jform_sprepeat0.checked = true;');
		}
		else{ datetimeset(); }
	}
	
	// Allow all
	function allowall(){
		var me = document.getElementById('allrequest');
		var form = document.getElementsByClassName('samcats');
		var target = document.getElementById('jform_sprequests');
		var result = 'all';
			
		if (me.checked == true){
			for (var i = 0; i < form.length; i++ ){ form[i].checked = true; }
			document.getElementById('blockrequest').checked = false;
			target.value = result;
		}
		return false;
	}
	
	// Block all
	function blockall(){
		var me = document.getElementById('blockrequest');
		var form = document.getElementsByClassName('samcats');
		var target = document.getElementById('jform_sprequests');
		var result = 'none';
		
		if (me.checked == true){ 
			for (var i = 0; i < form.length; i++ ){ form[i].checked = false; }
			document.getElementById('allrequest').checked = false;
			target.value = result;
		}
		else if(me.checked == false){
			var catt = document.getElementsByClassName('jse0');
			var vallu = '';
			for (var i = 0; i < catt.length; i++ ){ catt[i].checked = true; vallu += catt[i].value + ''; }
			if (vallu == ''){ vallu = 'none,'; }
			var minnu = vallu.length;
			vallu = vallu.slice(0,minnu-1);
			var ress = vallu;
			target.value = ress;
		}
		return false;
	}
		
	// Add Category
	function addme(){
		var target = document.getElementById('jform_sprequests');
		var form = document.getElementsByClassName('samcats');
		var cbResults = '';
		var total = 0;
		for (var i = 0; i < form.length; i++ ){
			if (form[i].checked == true){ cbResults += form[i].value + ''; total++; }
		}
		
		var prebuild = cbResults.length;
		cbResults = cbResults.slice(0,prebuild-1);
		var result = cbResults;
		if (result == ''){ result = 'none'; }
		
		if (total == form.length){
			document.getElementById('allrequest').checked = true;
			target.value = 'all';
		}
		else if (result == 'none'){
			document.getElementById('blockrequest').checked = true;
			target.value = result;
		}
		else{
			document.getElementById('allrequest').checked = false;
			document.getElementById('blockrequest').checked = false;
			target.value = result;
		}
		return false;
	}
	
	// Activate On Document load
	function sponload(){
		
		checkdefalt();
		
		var target = document.getElementById('jform_sprequests');
		var resall = "all";
		var resnone = "none";
		var str = target.value;
		var nofirst = str.substring(1, str.length-1);
		var arr = new Array();
		var errno = 0;
		arr = nofirst.split("','");
		if (target.value == resall){
			document.getElementById('allrequest').checked = true;
			allowall();
		}
		else if (target.value == resnone){
			document.getElementById('blockrequest').checked = true;
			blockall();
		}
		else{
			for (var i = 0; i < arr.length; i++ ) {
				var valu = arr[i];
				var chkbox = document.getElementById('sct_'+valu);
				if (chkbox != null){ chkbox.checked = true; }
				else{ errno++; }
			}
			var sambox = document.getElementsByClassName('samcats');
			var outp = '';
			
			for (var s = 0; s < sambox.length; s++ ) {
				if (sambox[s].checked == true){ outp += sambox[s].value + ''; }
			}
			var prebuild = outp.length;
			if (errno != 0){ alert("<?php echo JText::_('COM_JOOSAM_JSCRIPT_CATERR'); ?>"); }
			outp = outp.slice(0,prebuild-1);
			var output = outp;
			if (output == ''){ document.getElementById('blockrequest').checked = true; output = 'none'; }
			target.value = output;
		}
	}

	// DateTime Function
	function datetimeset(){
		
		// Start DateTime
		window.addEvent('domready', function() {Calendar.setup({
			inputField: "jform_spstart",
			ifFormat: "%Y-%m-%d %H:%M",
			button: "jform_spstart_img",
			align: "Tl",
			showsTime: true,
			singleClick: true,
			firstDay: 0
		});});
	
		// End DateTime
		window.addEvent('domready', function() {Calendar.setup({
			inputField: "jform_spends",
			ifFormat: "%Y-%m-%d %H:%M",
			button: "jform_spends_img",
			align: "Tl",
			showsTime: true,
			singleClick: true,
			firstDay: 0
		});});
	
		document.getElementById('jform_spstart').setAttribute('readonly','true');
		document.getElementById('jform_spends').setAttribute('readonly','true');
		document.getElementById('jform_spstart_img').setAttribute('style','cursor:pointer;');
		document.getElementById('jform_spends_img').setAttribute('style','cursor:pointer;');
	}

	// Make Onload Function
	document.onload = sponload();
</script>